var elUsername = document.getElementById('username'),
    elMsg      = document.getElementById('feedback');

var xhr = '';

function checkUsername(username){
    if(username === '' || username === null || username.length === 0)
    {
        elMsg.textContent = '用户名不能为空';
        return;
    }
    elMsg.textContent = '';
    sendRequest(username);
}

function sendRequest(username)
{
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = process;
    xhr.open('GET', 'usernameIsExist?username='+username);
    xhr.send(null);
}

function process(){
    if(xhr.readyState === 4)
    {
        if(xhr.status === 200)
        {
            var responseInfo = xhr.responseText;
            if(responseInfo === "Exist")
            {
                elMsg.textContent = '用户名已存在';
            }
            else
            {
                elMsg.textContent = '';
            }
        }
    }
}

elUsername.addEventListener("blur", function (){
    var username = elUsername.value.trim();
    checkUsername(username);
});